package com.toddsoft.toddtris.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;

import com.toddsoft.toddtris.model.Block;
import com.toddsoft.toddtris.view.WellRenderer;

public class AppletWellRenderer extends WellRenderer {

	private static final int X_OFFSET = 20;
	private static final int Y_OFFSET = 0;

	private static final int BLOCK_WIDTH = 21;
	private static final int BLOCK_HEIGHT = 21;

	private static final int GAME_HEIGHT = 438;
	private static final int GAME_WIDTH = 439;

    private Image[] blockPics = new Image[7];
    private Image[] deadBlockPics = new Image[8];
    private Image backGround;

    /* The colour of the background to the HTML page this applet is on */
    private Color backgroundColour = Color.black;
    
    private Applet applet;
    private MediaTracker mediaTracker;
    private int nextMediaID = 0;
	
    private Image getImage(String fileName) {
    	Image image = applet.getImage(applet.getCodeBase(), fileName);
    	mediaTracker.addImage(image, nextMediaID++);
    	return image;
    }
    
    public AppletWellRenderer(Applet applet) {
    	
    	this.applet = applet;
        this.mediaTracker = new MediaTracker(applet);
    	
        /* get the background color parameter */
        String backgroundColourStr = applet.getParameter("backgroundcolour");
        if (backgroundColourStr != null) {
            int backgroundColourInt = Integer.parseInt(backgroundColourStr, 16);
            backgroundColour = new Color(backgroundColourInt);
        }

        //load all the block images
        for (int i = 0; i < Block.NUM_BLOCKS; ++i) {
        	blockPics[i] = getImage("images/block" + i + ".gif");
        	deadBlockPics[i] = getImage("images/deadBlock" + i + ".gif");
        }
        //generic dead block for lines added on
        deadBlockPics[Block.NUM_BLOCKS] = getImage("images/deadblock.gif");

        //load background image
        backGround = getImage("images/background.jpg");

        //wait for all the images to be loaded
        try {
        	mediaTracker.waitForAll();
        } catch (InterruptedException e) {
        }

	}
    
	protected void drawElem(int blockNum, int x, int y, Object obj) {
		Graphics g = (Graphics)obj;
		if (blockNum == 0) {
            g.setColor(Color.black);
            g.fillRect(X_OFFSET + x*BLOCK_WIDTH,
                        Y_OFFSET + y*BLOCK_HEIGHT,
                        BLOCK_WIDTH,
                        BLOCK_HEIGHT);
            return;
        } else if (blockNum < 0) {
            g.drawImage(deadBlockPics[-1 - blockNum], 
                         X_OFFSET + x*BLOCK_WIDTH,
                         Y_OFFSET + y*BLOCK_HEIGHT,
                         applet);
        } else {
            g.drawImage(blockPics[blockNum - 1],
                         X_OFFSET + x*BLOCK_WIDTH,
                         Y_OFFSET + y*BLOCK_HEIGHT,
                         applet);
        }

	}

	protected void showBackground(Object obj) {
		Graphics g = (Graphics)obj;
        g.setColor(backgroundColour);
        g.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); 
        g.drawImage(backGround, 0, 0, applet);

        /* write "Time: " at the bottom */
        g.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 20));
        g.setColor(Color.red);
        g.drawString("Time: ", 25, 430);
        
	}

	protected void showLines(int lines, Object obj) {
		Graphics g = (Graphics)obj;
        g.setColor(Color.black);
        g.fillRect(311,141, 48, 28); 
        g.setColor(Color.red);
        g.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 12));
        g.drawString("" + lines, 315, 160);
	}

	protected void showNext(Block nextBlock, Object obj) {
		Graphics g = (Graphics)obj;
        g.setColor(Color.black);
        g.fillRect(300, 227, BLOCK_WIDTH*4, BLOCK_HEIGHT*4);

        int size = nextBlock.getSize();
        int xShift = 0;
        int yShift = 0;

        if (size == 4) {
            yShift = (int)(BLOCK_WIDTH / 2);
        }
        if (size == 3) {
            xShift = (int)(BLOCK_HEIGHT / 2);
        }
        if (size == 2) {
            xShift = BLOCK_WIDTH;
            yShift = BLOCK_HEIGHT;
        }
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                int squareNum = nextBlock.getElem(i, j);
                if (squareNum != 0) {
                    g.drawImage(blockPics[squareNum - 1],
                                 300 + xShift + i*BLOCK_WIDTH, 
                                 227 + yShift + j*BLOCK_HEIGHT,
                                 applet);
                }
            }
        }
	}

	protected void showTime(int secs, Object obj) {
		Graphics g = (Graphics)obj;
        g.setColor(backgroundColour);
        g.fillRect(95, 410,70, 27); 
        g.setFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 20));
        g.setColor(Color.red);
        g.drawString(formatTime(secs), 95, 430);
	}

}
